package com.boot.mgt.service.impl

import com.alibaba.fastjson.JSON
import com.boot.config.ParamConfig
import com.boot.core.result.ResultTo
import com.boot.core.task.ObjectTask
import com.boot.core.util.PositionApi
import com.boot.mgt.dao.mysql.TaAcdFile2018Mapper
import com.boot.mgt.dao.mysql.TaOndutyAccidentMapper
import com.boot.mgt.dao.mysql.TaOndutyMobileMapper
import com.boot.mgt.dao.mysql.TaPoliceMapper
import com.boot.mgt.model.TaAccident
import com.boot.mgt.model.TaAcdFile2018
import com.boot.mgt.service.TaAcdFile2018Service
import com.github.pagehelper.PageHelper
import com.github.pagehelper.PageInfo
import okhttp3.Request
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import tk.mybatis.mapper.entity.Example
import java.text.SimpleDateFormat
import java.util.*

@Service
class TaAcdFile2018ServiceImpl : TaAcdFile2018Service {

    @Autowired
    protected lateinit var taAcdFile2018Mapper: TaAcdFile2018Mapper

    @Autowired
    private lateinit var positionApi: PositionApi
    
    @Autowired
    private lateinit var paramConfig: ParamConfig


    override fun list(page_index: Int, page_size: Int, has_marked: Int, begin_date: Date, end_date: Date, sglx: Int): ResultTo {

        val list = with(TaAcdFile2018()) {
            PageHelper.startPage<Any>(page_index, page_size)
            val example = Example(TaAcdFile2018::class.java)
            val markedCriteria = example.createCriteria()

            if (has_marked == 1) {
                markedCriteria.andIsNotNull("latitude")
                markedCriteria.andIsNotNull("longitude")
            } else {
                markedCriteria.orIsNull("latitude")
                markedCriteria.orIsNull("longitude")
            }


            val example2 = Example(TaAcdFile2018::class.java)
            val dateCriteria = example2.createCriteria()
            dateCriteria.andBetween("sgfssj", begin_date, end_date)

            val example3 = Example(TaAcdFile2018::class.java)
            val xzqhCriteria = example3.createCriteria()
            xzqhCriteria.andEqualTo("xzqh", "610106")

            example.and(dateCriteria)
            example.and(xzqhCriteria)

            val exampleSGLX = Example(TaAcdFile2018::class.java)
            val sglxCriteria = exampleSGLX.createCriteria()
            if (sglx != -1) {
                sglxCriteria.andEqualTo("sglx", sglx)
                example.and(sglxCriteria)
            }

            taAcdFile2018Mapper.selectByExample(example)

        }
        return ResultTo().apply {
            val pageInfo = PageInfo(list)
            this["acd_file_list"] = list
            this["page_index"] = page_index
            this["page_amount"] = pageInfo.pages
            this["page_total"] = pageInfo.total
        }
    }

    override fun setCoordinates(acdFile2018Id: Int, latitude: String, longitude: String): ResultTo {
        var targetAcdFile = TaAcdFile2018()
        targetAcdFile.id = acdFile2018Id
        targetAcdFile.latitude = latitude.toDouble()
        targetAcdFile.longitude = longitude.toDouble()
        taAcdFile2018Mapper.updateByPrimaryKeySelective(targetAcdFile)

        return ResultTo()
    }

    override fun detail(acdFile2018Id: Int): ResultTo {
        var targetAcdFile = taAcdFile2018Mapper.selectByPrimaryKey(acdFile2018Id)

        return ResultTo().apply {
            this["acd_file"] = targetAcdFile
        }
    }

    @Throws(Exception::class)
    override fun selectPosition(taAcdFile2018: TaAcdFile2018) {
        positionApi.send(taAcdFile2018.longitude, taAcdFile2018.latitude)?.apply {
            taAcdFile2018.geojsonList = this.geojsonList
            taAcdFile2018.locationIdList = this.locationIdList
            taAcdFile2018.districtId = this.districtId
            taAcdFile2018.streetId = this.streetId
            taAcdFile2018.addressId = this.addressId
            taAcdFile2018Mapper.updateByPrimaryKeySelective(taAcdFile2018)
        }
    }

    companion object {
        private val log: Logger = LoggerFactory.getLogger(this::class.java)
    }

}